package com.open.lua;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.keplerproject.luajava.LuaException;
import org.keplerproject.luajava.LuaState;
import org.keplerproject.luajava.LuaStateFactory;

import com.example.androidluatest.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class TestActivity extends Activity {
	/** Lua解析和执行由此对象完成 */
	private LuaState mLuaState;

	private Button mLuaCallAndroidButton;
	private Button mLuaCallAndroidSetButton;
	private Button mLaunchIntentButton;
	private Button mAddBtnButton;
	private Button mLoadLibButton;
	
	private LinearLayout mLayout;
	private static int count;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);

		initView();
		setListener();

		mLuaState = LuaStateFactory.newLuaState();
		mLuaState.openLibs();
		mLuaState.getPlatform();
		
		try {
			LoadLibExample.open(mLuaState);
		} catch (LuaException e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 初始化控件
	 */
	private void initView(){
		mLayout = (LinearLayout) findViewById(R.id.layout);
		mLuaCallAndroidButton = (Button)findViewById(R.id.callAndroidApi);
		mLuaCallAndroidSetButton = (Button)findViewById(R.id.callAndroidSetApi);
		mLaunchIntentButton = (Button)findViewById(R.id.launchIntent);
		mAddBtnButton = (Button)findViewById(R.id.add_btn);
		mLoadLibButton = (Button)findViewById(R.id.load_lib_btn);
	}
	
	/**
	 * 设置监听
	 */
	private void setListener(){
		mLuaCallAndroidButton.setOnClickListener(listener);
		mLuaCallAndroidSetButton.setOnClickListener(listener);
		mLaunchIntentButton.setOnClickListener(listener);
		mAddBtnButton.setOnClickListener(listener);
		mLoadLibButton.setOnClickListener(listener);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.callAndroidApi:
				callAndroidAPI();
				break;
			case R.id.callAndroidSetApi:
				callAndroidAPISet();
				break;
			case R.id.launchIntent:
				launchIntent();
				break;
			case R.id.add_btn:
				addButton();
				break;
			case R.id.load_lib_btn:
				LoadLib();
				break;
			}
		}

	};

	
	/**
	 * 启动Intent
	 */
	private void launchIntent(){
		try {

			mLuaState.LdoString(readStream(getResources().openRawResource(
					R.raw.luafile)));
			// 找到functionInLuaFile函数
			mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "launchIntent");

			mLuaState.pushJavaObject(getApplicationContext());
			mLuaState.call(1, 0);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/** 
	 * 添加按钮，并为按钮设置监听
	 */
	protected void addButton() {
		try {

			mLuaState.LdoString(readStream(getResources().openRawResource(
					R.raw.luafile)));
			// 找到functionInLuaFile函数
			mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "addButton");
			mLuaState.pushJavaObject(getApplicationContext());// 第一个参数 context
			mLuaState.pushJavaObject(mLayout);//第二个参数， Layout
			mLuaState.call(2, 0);// 2个参数，0个返回值
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	
	/** 
	 * Lua脚本使用LoadLib回调Java,并传多个参数 
	 */
	protected void LoadLib() {
		try {
			mLuaState.LdoString(readStream(getResources().openRawResource(
					R.raw.luafile)));
			// 找到functionInLuaFile函数
			mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "callback");
			mLuaState.call(0, 0);// 0个参数，0个返回值
		} catch (Exception e) {
			e.printStackTrace();

		}
	}


	/**
	 * 启动Android的"设置"界面
	 */
	private void callAndroidAPISet() {
		mLuaState.LdoString(readStream(getResources().openRawResource(
				R.raw.luafile)));
		mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "launchSetting");
		mLuaState.pushJavaObject(getApplicationContext());
		mLuaState.call(1, 0);
	}

	/**
	 * lua中调用android的api测试
	 */
	private void callAndroidAPI() {
		mLuaState.LdoString(readStream(getResources().openRawResource(
				R.raw.luafile)));
		mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "callAndroidApi");// 找到functionInLuaFile函数
		mLuaState.pushJavaObject(getApplicationContext());
		mLuaState.pushJavaObject(mLayout);
		mLuaState.pushString("lua call android Textview的setText()方法, 内容是:"
				+ (++count));
		mLuaState.call(3, 0);
	}

	/**
	 * 读取lua脚本
	 * 
	 * @param is
	 * @return
	 */
	private String readStream(InputStream is) {
		try {
			ByteArrayOutputStream bo = new ByteArrayOutputStream();
			int i = is.read();
			while (i != -1) {
				bo.write(i);
				i = is.read();
			}
			return bo.toString();
		} catch (IOException e) {
			Log.e("ReadStream", "读取文件流失败");
			return "";
		}
	}
}